﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using IDAL;
namespace AccessDAL {
    public partial class ContactGroup :IContactGroup {
        public ContactGroup() { }
        /// <summary>
        /// 查询并返回分组列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataTable GetList(string strWhere) {
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from contactgroup");
            if(strWhere.Trim() != "") {
                sb.Append(" where " + strWhere);
            }
            return OleDbHelper.ExecuteDataTable(sb.ToString());
        }
        /// <summary>
        /// 添加新的分组记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(Model.ContactGroup model) {
            StringBuilder sb = new StringBuilder();
            sb.Append("insert into contactgroup (groupname,[memo]) values (@groupname,@memo)");
            OleDbParameter[] parameters = {
                new OleDbParameter("@groupname",OleDbType.VarChar,50),
                new OleDbParameter("@memo",OleDbType.VarChar,255)
            };
            parameters[0].Value = model.GroupName;
            parameters[1].Value = model.Memo;
            int rows = OleDbHelper.ExecuteNonQuery(sb.ToString(),parameters);
            if (rows == 1)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 根据分组ID获取分组信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Model.ContactGroup GetModel(int Id) {
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from contactgroup");
            sb.Append(" where id = @id");
            OleDbParameter[] parameters = {
                new OleDbParameter("@id",OleDbType.Integer)
            };
            parameters[0].Value = Id;
            Model.ContactGroup model = new Model.ContactGroup();
            DataTable dt = new DataTable();
            dt = OleDbHelper.ExecuteDataTable(sb.ToString(), parameters);
            if (dt.Rows.Count == 1) {
                model.Id = Convert.ToInt32(dt.Rows[0]["id"]);
                model.GroupName = dt.Rows[0]["groupname"].ToString();
                if (dt.Rows[0]["memo"] != null) {
                    model.Memo = dt.Rows[0]["memo"].ToString();
                }
                return model;
            }
            else {
                return null;
            }
        }
        /// <summary>
        /// 根据分组名称，获取分组信息
        /// </summary>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public Model.ContactGroup GetModel(string groupName) {
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from contactgroup");
            sb.Append(" where groupname = @groupname");
            OleDbParameter[] parameters = {
                new OleDbParameter("@groupname",OleDbType.VarChar)
            };
            parameters[0].Value = groupName;
            Model.ContactGroup model = new Model.ContactGroup();
            DataTable dt = new DataTable();
            dt = OleDbHelper.ExecuteDataTable(sb.ToString(), parameters);
            if (dt.Rows.Count == 1) {
                model.Id = Convert.ToInt32(dt.Rows[0]["id"]);
                model.GroupName = dt.Rows[0]["groupname"].ToString();
                if (dt.Rows[0]["memo"] != null) {
                    model.Memo = dt.Rows[0]["memo"].ToString();
                }
                return model;
            }
            else {
                return null;
            }
        }
        /// <summary>
        /// 根据分组编号，删除分组
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool Delete(int Id) {
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from contactgroup where id = @id");
            OleDbParameter[] parameters = { new OleDbParameter("@id", OleDbType.Integer) };
            parameters[0].Value = Id;
            int n = OleDbHelper.ExecuteNonQuery(sb.ToString(), parameters);
            if (n == 1)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 更新分组信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(Model.ContactGroup model) {
            StringBuilder sb = new StringBuilder();
            sb.Append("update contactgroup set groupname = @groupname,[memo] = @memo");
            sb.Append(" where id = @id");
            OleDbParameter[] parameters = {
                new OleDbParameter("@groupname",OleDbType.VarChar,50),
                new OleDbParameter("@memo",OleDbType.VarChar,255),
                new OleDbParameter("@id",OleDbType.Integer)
            };
            parameters[0].Value = model.GroupName;
            parameters[1].Value = model.Memo;
            parameters[2].Value = model.Id;
            int n = OleDbHelper.ExecuteNonQuery(sb.ToString(), parameters);
            if (n == 1)
                return true;
            else
                return false;
        }
    }
}
